package cn.com.homeyoda.aimate.aihealth.ui.activity;

import android.app.Application;
import android.util.Log;

import androidx.annotation.NonNull;

import com.wzq.mvvmsmart.binding.command.BindingAction;
import com.wzq.mvvmsmart.binding.command.BindingCommand;
import com.wzq.mvvmsmart.event.StateLiveData;
import com.wzq.mvvmsmart.utils.KLog;
import com.wzq.mvvmsmart.utils.RxUtils;

import cn.com.homeyoda.aimate.aihealth.base.BaseViewModel;
import cn.com.homeyoda.aimate.aihealth.bean.BaseResponse2;
import cn.com.homeyoda.aimate.aihealth.bean.QuestionnaireBean;
import cn.com.homeyoda.aimate.aihealth.bean.QuestionnaireNetBean;
import cn.com.homeyoda.aimate.aihealth.data.Repository;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * Created By 刘纯贵
 * Created Time 2020/4/30
 */
public class NetworkTestModel extends BaseViewModel<Repository> {

    private static final String TAG = "pangtao";
    public StateLiveData<BaseResponse2<Object>> netStateLiveData;

    public NetworkTestModel(@NonNull Application application, Repository model) {
        super(application, model);
        netStateLiveData = new StateLiveData();
    }


    /**
     * 获取体检引导视频链接
     */
    public BindingCommand getVideoBt = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            Log.i(TAG, "获取体检引导视频链接");
            model.getVideoUrl()
                    .compose(RxUtils.observableToMain())
                    .compose(RxUtils.exceptionTransformer())
                    .doOnSubscribe(NetworkTestModel.this)
                    .subscribe(new Observer<BaseResponse2<Object>>() {
                        @Override
                        public void onSubscribe(Disposable d) {
                            stateLiveData.postLoading();
                        }

                        @Override
                        public void onNext(BaseResponse2<Object> objectBaseResponse2) {
                            if (objectBaseResponse2.getCode() == 200) {
                                KLog.d(TAG, "onNext objectBaseResponse2 = " + objectBaseResponse2);
                                stateLiveData.setValue(objectBaseResponse2);

                            }
                        }


                        @Override
                        public void onError(Throwable e) {
                            KLog.e("进入onError" + e.getMessage());
                            stateLiveData.postError();
                        }

                        @Override
                        public void onComplete() {
                            KLog.e("进入onComplete");
                            stateLiveData.postSuccess();
                            //stateLiveData.postIdle();
                        }
                    });


        }
    });


    /**
     * 获取问卷调查列表
     */
    public BindingCommand getQuestionnaireList = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            Log.i(TAG, "获取问卷调查列表");
            model.getQuestionnaireList(10,1)
                    .compose(RxUtils.observableToMain())
                    .compose(RxUtils.exceptionTransformer())
                    .doOnSubscribe(NetworkTestModel.this)
                    .subscribe(new Observer<QuestionnaireNetBean>() {
                        @Override
                        public void onSubscribe(Disposable d) {
                            stateLiveData.postLoading();
                        }

                        @Override
                        public void onNext(QuestionnaireNetBean objectBaseResponse2) {
                            if (objectBaseResponse2.getCode() == 200) {
                                KLog.d(TAG, "onNext objectBaseResponse2 = " + objectBaseResponse2);
                                stateLiveData.setValue(objectBaseResponse2);

                            }
                        }


                        @Override
                        public void onError(Throwable e) {
                            KLog.e("进入onError" + e.getMessage());
                            stateLiveData.postError();
                        }

                        @Override
                        public void onComplete() {
                            KLog.e("进入onComplete");
                            stateLiveData.postSuccess();
                            //stateLiveData.postIdle();
                        }
                    });



        }
    });


}
